package com.starbaba.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

	/**
	 * 根据开始时间和结束时间返回时间段内的时间集合
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return List
	 */
	public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {

		List<Date> result = new ArrayList<Date>();
		result.add(beginDate);
		Calendar cal = Calendar.getInstance();
		cal.setTime(beginDate);

		boolean bContinue = true;
		while (bContinue) {
			cal.add(Calendar.DAY_OF_MONTH, 1);
			if (endDate.after(cal.getTime())) {
				result.add(cal.getTime());
			} else {
				break;
			}
		}

		if (endDate.after(beginDate)) {
			result.add(endDate);
		}
		return result;
	}

	/**
	 * 根据传入日期获取日期所在的月，返回月开始与结束
	 * 
	 * @param statDate
	 * @return
	 * @throws ParseException
	 */
	public static String dayGetMonth(String statDate) throws ParseException {
		
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		calendar.setTime(sdf.parse(statDate));
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		String monthStart = sdf.format(calendar.getTime());
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		String monthEnd = sdf.format(calendar.getTime());
		return monthStart + "~" + monthEnd;
	}

	/**
	 * 
	 * @param statDate
	 * @return 一个星期的开始与结束 周期：每周四到每周三
	 * @throws ParseException
	 */
	public static String getWeekByDate(String statDate) throws ParseException {

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		calendar.setTime(sdf.parse(statDate));
		int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0) {
			week = 7;
		}

		int toLast4 = 0;
		if (week > 4) {
			toLast4 = week - 4;
		} else if (week < 4) {
			toLast4 = week + 3;
		}

		calendar.add(Calendar.DAY_OF_MONTH, -toLast4);
		String weekStart = sdf.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, 6);
		String weekEnd = sdf.format(calendar.getTime());
		return weekStart + "~" + weekEnd;
	}
	
	
	/**
	 * 计算时间范围内Week时间
	 * 
	 * @param begin
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getWeekList(String begin, String end) throws ParseException {

		List<String> resutList = new ArrayList<String>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String week = getWeekByDate(begin);
		resutList.add(week);
		begin = week.substring(week.indexOf("~") + 1);

		Date startDate = format.parse(begin);
		Date endDate = format.parse(end);

		boolean isfinaish = true;

		while (isfinaish) {
			if (startDate.compareTo(endDate) != -1) {
				isfinaish = false;
				break;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(format.parse(begin));
			calendar.add(Calendar.DAY_OF_MONTH, 1); // 日期+1天
			Date tomorrow = calendar.getTime();
			week = getWeekByDate(format.format(tomorrow));
			resutList.add(week);
			begin = week.substring(week.indexOf("~") + 1);
			startDate = format.parse(begin);
		}
		return resutList;
	}
	
	
	/**
	 * 计算时间范围内Month时间
	 * 
	 * @param begin
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getMonthList(String begin, String end) throws ParseException {

		List<String> resutList = new ArrayList<String>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String month = dayGetMonth(begin);
		resutList.add(month);
		begin = month.substring(month.indexOf("~") + 1);

		Date startDate = format.parse(begin);
		Date endDate = format.parse(end);

		boolean isfinaish = true;

		while (isfinaish) {
			if (startDate.compareTo(endDate) != -1) {
				isfinaish = false;
				break;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(format.parse(begin));
			calendar.add(Calendar.DAY_OF_MONTH, 1); // 日期+1天
			Date tomorrow = calendar.getTime();
			month = dayGetMonth(format.format(tomorrow));
			resutList.add(month);
			begin = month.substring(month.indexOf("~") + 1);
			startDate = format.parse(begin);
		}
		return resutList;
	}
}
